﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Common.Tools.Encrypt
{
    public class DES
    {
        #region DES 对称加密
        /// <summary>
        /// DES 加密、解密密钥(8位)
        /// </summary>
        private static string DESKEY = "XMKBAJSO".Substring(0, 8);
        private static byte[] rgbKey = ASCIIEncoding.ASCII.GetBytes(DESKEY);
        private static byte[] rgbIV = ASCIIEncoding.ASCII.GetBytes(DESKEY.Insert(1, "c"));
        /// <summary>
        /// DES加密
        /// </summary>
        /// <param name="Text">要进行加密的字符串</param>
        /// <returns>加密后的字符串</returns>
        public static string Encrypt(string Text)
        {
            string result;
            using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
            {
                des.Key = rgbKey;
                des.IV = rgbIV;
                result = Encrypt(Text, des);
            }
            return result;
        }
        /// <summary>
        /// DES加密
        /// </summary>
        /// <param name="pToEncrypt">要加密的字符串。</param>
        /// <param name="sKey">密钥，且必须为8位。</param>
        /// <returns>以Base64格式返回的加密字符串。</returns>
        public static string Encrypt(string pToEncrypt, string sKey)
        {
            string result;
            using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
            {
                des.Key = Encoding.ASCII.GetBytes(sKey.Substring(0, 8));
                des.IV = Encoding.ASCII.GetBytes(sKey.Insert(1, "c").Substring(0, 8));
                result = Encrypt(pToEncrypt, des);
            }
            return result;
        }
        private static string Encrypt(string pToEncrypt, DESCryptoServiceProvider des)
        {
            byte[] inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt);
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
            {
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
            }
            return Convert.ToBase64String(ms.ToArray());
        }
        /// <summary>
        /// DES解密
        /// </summary>
        /// <param name="Text">要进行解密的字符串</param>
        /// <returns>解密后的字符串</returns>
        public static string Decrypt(string Text)
        {
            string result;
            using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
            {
                des.Key = rgbKey;
                des.IV = rgbIV;
                result = Decrypt(Text, des);
            }
            return result;
        }
        /// <summary>
        /// DES解密
        /// </summary>
        /// <param name="pToDecrypt">要解密的以Base64</param>
        /// <param name="sKey">密钥，且必须为8位。</param>
        /// <returns>已解密的字符串。</returns>
        public static string Decrypt(string pToDecrypt, string sKey)
        {
            string result;
            using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
            {
                des.Key = Encoding.ASCII.GetBytes(sKey.Substring(0, 8));
                des.IV = Encoding.ASCII.GetBytes(sKey.Insert(1, "c").Substring(0, 8));
                result = Decrypt(pToDecrypt, des);
            }
            return result;
        }
        private static string Decrypt(string pToDecrypt, DESCryptoServiceProvider des)
        {
            byte[] inputByteArray = Convert.FromBase64String(pToDecrypt);
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
            {
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
            }
            return Encoding.UTF8.GetString(ms.ToArray());
        }
        /// <summary>
        /// 创建DES密钥
        /// </summary>
        /// <returns>密钥</returns>
        public static string GenerateKey()
        {
            DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)System.Security.Cryptography.DES.Create();
            return Encoding.ASCII.GetString(desCrypto.Key);
        }
        #endregion
    }
}
